import java.util.Arrays;

public class Test02 {
    public static String countAndSay(int n) {
        //外观数列
        //countAndSay(1) = "1"
        String ret = "1";
        //模拟n - 1次
        for (int i = 1; i < n; i++) {
            //需要把每次的解释过程存储起来
            StringBuilder tmp = new StringBuilder();
            for (int left = 0,right = 0; right < ret.length();) {
                while (right < ret.length() && ret.charAt(left) == ret.charAt(right)){
                    right++;
                }
                //数字转化为字符串
                tmp.append(Integer.toString(right - left));
                tmp.append(ret.charAt(left));
                left = right;
            }
            ret = tmp.toString();
        }
        return ret;
    }

    public static void main(String[] args) {
        System.out.println(countAndSay(4));
    }
}
